//
//  FriendsListViewController.m
//  PHMall
//
//  Created by admin on 2018/1/18.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "FriendsListViewController.h"
#import "FriendsCell.h"

@interface FriendsListViewController ()<UITableViewDelegate,UITableViewDataSource,FriendsCellDelegate>
@property(nonatomic,strong)UITableView *myTableview;
@property(nonatomic,strong)NSMutableArray *friends;
@property(nonatomic,strong)UIButton *select;
@end

@implementation FriendsListViewController
-(void)rightBarAction{
    NSMutableArray *list=[[NSMutableArray alloc]init];
    for (FriendsModel *model in self.friends) {
        model.select ? [list addObject:model]:nil;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(ChooseFriend:)]) {
        [self.delegate ChooseFriend:list];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self refreshFriends];
    [self.view addSubview:self.myTableview];
    [self.myTableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.left.right.equalTo(self.view).priorityHigh();
        make.bottom.equalTo(self.view).with.offset(-self.tabBar_height).priorityHigh();
    }];
    
    UIBarButtonItem*  rightBar=[[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarAction)];
    rightBar.tintColor=[UIColor blackColor];
    self.navigationItem.rightBarButtonItem=rightBar;
    
    UIBarButtonItem* leftBar=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarAction)];
    self.navigationItem.leftBarButtonItem=leftBar;
    // Do any additional setup after loading the view.
}
-(void)clickShopAllChooseButton{
    
    for (FriendsModel *model in self.friends) {
        model.select = !self.select.selected;
    }
    [self.myTableview reloadData];
}
- (void)refreshFriends{
    
    NSString *urlstr =  [NSString stringWithFormat:@"%@%@",HomeURL,@"sms_send"];
    [httpTool ZBPostNetDataWith:urlstr withDic:nil andSuccess:^(id responseObject) {
//        [self.messageArray removeAllObjects];
        NSMutableDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        if ([[NSString stringWithFormat:@"%@",responseDict[@"ret"]] isEqualToString:@"200"]) {
            NSMutableArray *data=responseDict[@"data"];
            for (NSMutableDictionary *dict in data) {
                FriendsModel *model=[FriendsModel yy_modelWithDictionary:dict];
                model.select=NO;
                [self.friends addObject:model];
            }
            [self.myTableview reloadData];
//            [self.hud hideAnimated:YES];
        }
    } andFailure:^{
//        [self.hud hideAnimated:YES];
    }];
    
    
}
-(void)selectedAction:(UITableViewCell *)cell{
    NSIndexPath *index=[self.myTableview indexPathForCell:cell];
    FriendsModel *model=self.friends[index.row];
    model.select=!model.select;
    
    BOOL allChoosen = YES;
    for (FriendsModel *model in self.friends) {
        allChoosen &= model.select;
    }
    self.select.selected = allChoosen;
    [self.myTableview reloadData];
}
#pragma mark - tableView
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.friends.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,44)];
    headerView.backgroundColor=[UIColor groupTableViewBackgroundColor];
    
    self.select =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.select setImage:[UIImage imageNamed:[NSString stringWithFormat:@"未选中"]] forState:UIControlStateNormal];
    [self.select setImage:[UIImage imageNamed:[NSString stringWithFormat:@"选中"]] forState:UIControlStateSelected];
    [self.select addTarget:self action:@selector(clickShopAllChooseButton) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:self.select];
    [self.select mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headerView).offset(0);
        make.left.equalTo(headerView).with.offset(5);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(30);
    }];
    BOOL allChoosen = YES;
    for (FriendsModel *model in self.friends) {
        allChoosen &= model.select;
    }
    self.select.selected = allChoosen;
    
    UILabel *name=[UILabel new];
    name.text=@"全选";
    name.font=[UIFont systemFontOfSize:15];
    [headerView addSubview:name];
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.select);
        make.left.equalTo(self.select.mas_right).with.offset(5);
    }];
    
    return headerView;
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FriendsCell *cell=[FriendsCell theShareCellWithTableView:tableView];
    FriendsModel *model=self.friends[indexPath.row];
    cell.model=model;
    cell.delegate=self;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    self.hidesBottomBarWhenPushed=YES;
//    MessageDetailsVC *evaluate=[[MessageDetailsVC alloc]init];
//    [self.navigationController pushViewController:evaluate animated:YES];
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    //分割线补全
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}
//MARK:-GETTER
- (UITableView *)myTableview{
    if (!_myTableview) {
        _myTableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableview.delegate = self;
        _myTableview.dataSource = self;
        _myTableview.backgroundColor=[UIColor groupTableViewBackgroundColor];
        _myTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableview.showsVerticalScrollIndicator=NO;
        _myTableview.scrollEnabled = YES;
    }
    return _myTableview;
}
-(NSMutableArray *)friends{
    if (!_friends) {
        _friends=[[NSMutableArray alloc]init];
    }
    return _friends;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
